package argo.hot100.recall;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CombinationSum {

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        backTrace(candidates, res, new ArrayList<>(), 0, target, 0);
        return res;

    }

    public void backTrace(int[] candidates, List<List<Integer>> res, List<Integer> subres, int sum, int target, int depth) {
        if (sum > target) {
            return;
        }
        if (sum == target) {
            res.add(new ArrayList<>(subres));
            return;
        }

        for (int i = depth; i < candidates.length; i++) {
            subres.add(candidates[i]);
            System.out.println(Arrays.toString(subres.toArray()));
            backTrace(candidates, res, subres, sum + candidates[i], target, i);
            subres.remove(subres.size() - 1);
            System.out.println(Arrays.toString(subres.toArray()));

        }
    }
}
